#include "nst1001.h"
#include "stm32l4xx_hal.h"
#include "tim.h"

float nst_data_sample(void)
{
	uint32_t counter = 0;
	float temp_data = 0.0f;
	int16_t temp_hex = 0;
	uint8_t read_num = 0;

	do
	{
		HAL_TIM_Base_Start(&htim2);

		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);

		__HAL_TIM_SET_COUNTER(&htim2, 0);

		HAL_Delay(80);

		HAL_TIM_Base_Stop(&htim2);

		counter = __HAL_TIM_GET_COUNTER(&htim2);

		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);

		temp_data = counter * 0.0625 - 50.0625;
		temp_hex = temp_data * 10;

		read_num++;
	} while((temp_hex <= -450) && (read_num < 3));

	return temp_data;
}

